package com.study.talk.ui.app.group.create.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import com.study.talk.R
import com.study.talk.ui.adapter.UsersSelectAdapter
import com.study.talk.ui.app.base.BaseFragment
import com.study.talk.databinding.FragmentInviteFriendBinding
import com.study.talk.ui.app.group.create.CreateGroupViewModel
import com.study.talk.utils.extras.showToast
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class InviteFriendFragment : BaseFragment() {

    private lateinit var binding: FragmentInviteFriendBinding

    private val adapter by lazy { UsersSelectAdapter() }

    private val parentViewModel by lazy { ViewModelProvider(requireActivity())[CreateGroupViewModel::class] }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View {
        binding = FragmentInviteFriendBinding.inflate(inflater, container, false)
        binding.startCreateGroupListView.layoutManager = LinearLayoutManager(requireContext())
        binding.startCreateGroupListView.adapter = adapter
        binding.startCreateGroupBt.setOnClickListener {
            val selectList = mutableListOf<Int>()
            (binding.startCreateGroupListView.adapter as UsersSelectAdapter).selectMap.entries.filter { it.value }.mapTo(selectList) { it.key }
            showToast("" + selectList.size)
            parentViewModel.changeSelectUsers(selectList)
            findNavController().navigate(R.id.action_inviteFriendFragment_to_createGroupInfoFragment)
        }
        app.friends.observe(viewLifecycleOwner){
            adapter.submitList(it)
        }
        return binding.root
    }
}